//
//  MCGuideController.m
//  QuKeClient
//
//  Created by delicious on 2018/2/28.
//  Copyright © 2018年 delicious. All rights reserved.
//

#import "MCGuideController.h"

#define LAST_RUN_VERSION_KEY @"version"


@interface MCGuideViewCell : UICollectionViewCell
@property (nonatomic, strong) UIImageView * imageView;
@property (nonatomic, strong) UIButton * button;
@property (nonatomic, assign) BOOL isHiddenBtn;
@end
@implementation MCGuideViewCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.imageView = ({
            UIImageView *v =[UIImageView new];
            v.contentMode = UIViewContentModeScaleAspectFill;
            v.layer.masksToBounds = YES;
            v.frame = CGRectMake(0, 0, frame.size.width, frame.size.height);
            [self addSubview:v];
            v;
        });
        
        
        self.button = ({
            UIButton *l = [UIButton buttonWithType:UIButtonTypeCustom];
            [l setBackgroundImage:[UIImage imageNamed:@"img_05"] forState:UIControlStateNormal];
            l.size = CGSizeMake(kFitW(416)/2,kFitH(50));
            l.bottom = frame.size.height-kFitH(70);
            l.centerX =frame.size.width/2;
            //            l.backgroundColor = [UIColor redColor];
            l.hidden = YES;
            [self addSubview:l];
            l;
        });
        
        
    }
    return self;
}




@end



@interface MCGuideController ()<UICollectionViewDataSource,UICollectionViewDelegate>
{
    UIPageControl *_page;
}

@property (nonatomic, copy) dispatch_block_t clickBlock;
@property (nonatomic, strong) NSMutableArray * dataArr;
@end

@implementation MCGuideController

+(NSString *)getLastRunVersion
{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    
    NSString *currentVersion = [[[NSBundle mainBundle] infoDictionary]
                                objectForKey:@"CFBundleShortVersionString"];
    
    return [userDefaults objectForKey:LAST_RUN_VERSION_KEY];

}

-(instancetype)initImages:(NSArray*)images CLickBlock:(dispatch_block_t)block
{
    self = [super init];
    if (self) {
        [self.dataArr addObjectsFromArray:images];
        self.clickBlock = block;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    CGSize size = [UIScreen mainScreen].bounds.size;
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.scrollDirection =UICollectionViewScrollDirectionHorizontal;
    layout.itemSize = CGSizeMake(size.width, size.height);
    layout.minimumLineSpacing =0;
    layout.minimumInteritemSpacing =0;
    
    UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0,size.width,size.height) collectionViewLayout:layout];
    [collectionView registerClass:[MCGuideViewCell class] forCellWithReuseIdentifier:@"cell"];
    collectionView.showsHorizontalScrollIndicator = NO;
    collectionView.bounces = NO;
    collectionView.pagingEnabled = YES;
    collectionView.dataSource = self;
    collectionView.delegate = self;
    [self.view addSubview:collectionView];
    
    UIPageControl *page = [[UIPageControl alloc]initWithFrame:CGRectZero];
    page.size = CGSizeMake(kFitW(50), 10);
    page.centerX = size.width/2;
    page.bottom = size.height-kFitH(42);
    page.numberOfPages = 3;
    page.pageIndicatorTintColor = hexCOLOR(@"#b3b1b1");
    page.currentPageIndicatorTintColor = [UIColor whiteColor];
    [self.view addSubview:page];
    _page =page;
    
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataArr.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    MCGuideViewCell *cell= [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    if (cell.isHiddenBtn==NO) {
         [cell.button addTarget:self action:@selector(onOpenLoginVC:) forControlEvents:UIControlEventTouchUpInside];
        cell.isHiddenBtn=YES;
    }
    id model = self.dataArr[indexPath.item];
   
    if ([model isKindOfClass:[NSString class]]) {
        cell.imageView.image = [UIImage imageNamed:model];
    }
    else if([model isKindOfClass:[UIImage class]])
    {
        cell.imageView.image = model;
    }
    else
    {
        
    }

    cell.button.hidden = self.dataArr.count-1==indexPath.item?NO:YES;
    return cell;
}

-(void)onOpenLoginVC:(UIButton *)button
{
    WSELF(weakSelf);
    [UIView transitionWithView:[UIApplication sharedApplication].keyWindow duration:1.f options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
        BOOL oldState = [UIView areAnimationsEnabled];
        [UIView setAnimationsEnabled:NO];
        if (weakSelf.clickBlock) {
            weakSelf.clickBlock();
        }
        [UIView setAnimationsEnabled:oldState];
    }
        completion:^(BOOL finished) {
    
            NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
            
            NSString *currentVersion = [[[NSBundle mainBundle] infoDictionary]
                                        objectForKey:@"CFBundleShortVersionString"];
            [userDefaults setObject:currentVersion forKey:LAST_RUN_VERSION_KEY];
            
            [userDefaults synchronize];
            
        }];
    
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    int p = scrollView.contentOffset.x/scrollView.frame.size.width;
    
    _page.currentPage = p;
    
    _page.hidden = p==3?YES:NO;
    
}

-(NSMutableArray *)dataArr
{
    if (!_dataArr) {
        _dataArr = [NSMutableArray new];
    }
    return _dataArr;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
